package com.ccp.dev.qbdms.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccp.dev.core.basic.base.BaseController;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.HighlightConst;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.model.WQbdMyKnowledge;
import com.ccp.dev.qbdms.model.WQbdMyPushRecord;
import com.ccp.dev.qbdms.model.WQbdQualityAnalysisReport;
import com.ccp.dev.qbdms.model.WQbdQualityKnow;
import com.ccp.dev.qbdms.service.WQbdMyKnowledgeService;
import com.ccp.dev.qbdms.service.WQbdMyPushRecordService;
import com.ccp.dev.qbdms.service.WQbdQualityAnalysisReportService;
import com.ccp.dev.qbdms.service.WQbdQualityKnowService;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import com.google.common.collect.Lists;
import org.apache.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(ZhiLiangConst.URL_API + ZhiLiangConst.URL_VERSION)
public class WQbdQualityKnowApiController extends BaseController {

    @Resource
    private WQbdQualityKnowService wQbdQualityKnowService;
    @Resource
    WQbdMyKnowledgeService wQbdMyKnowledgeService;
    @Resource
    WQbdMyPushRecordService wQbdMyPushRecordService;
    /**
     * 质量知识下发的接口
     *
     * @param jsonstr
     * @return
     */
    @PostMapping("/wQbdQualityKnow/reportSend")
    public Object reportSend(@RequestBody String jsonstr) {
        //解析json
        JSONObject jsonobj = JSONObject.parseObject(jsonstr);
        // 获取上报参数,是一个list
        JSONArray listobj = jsonobj.getJSONArray(ZhiLiangConst.API_REPORT_PARA_LIST);
        String OrgCode=jsonobj.getString("orgCode");
        List<WQbdQualityKnow> list = listobj.toJavaList(WQbdQualityKnow.class);
        boolean hasFile = jsonobj.containsKey(ZhiLiangConst.API_REPORT_PARA_LIST_FILE);
        List<ExtendSysFile> filelist= Lists.newArrayList();
        if(hasFile){
            JSONArray filelistjson = jsonobj.getJSONArray(ZhiLiangConst.API_REPORT_PARA_LIST_FILE);
            filelist = filelistjson.toJavaList(ExtendSysFile.class);
            ReadAttachmentUtil.getEntityData(list,filelist);
        }
        for(WQbdQualityKnow wQbdQualityKnow:list){
            WQbdQualityKnow qualityKnow = wQbdQualityKnowService.getOneById(wQbdQualityKnow.getId());
            //去重
            if(BeanUtils.isNotEmpty(qualityKnow)){
                wQbdQualityKnowService.delById(qualityKnow.getId());
//                ES
                wQbdQualityKnowService.delete(qualityKnow.getId());
            }
        }
        int result=wQbdQualityKnowService.saveSendReport(list,OrgCode);

        for(WQbdQualityKnow wQbdQualityKnow:list){
            //查找设置感兴趣的记录表  用户进行推送
            Map<String,Object> map=new HashMap<>();
            map.put("knowledge_classifica",wQbdQualityKnow.getKnowledgeClassification());
            List<WQbdMyKnowledge> wQbdMyKnowledgeList = wQbdMyKnowledgeService.queryListByMap(map);
            wQbdMyKnowledgeList.forEach(wQbdMyKnowledge -> {
                //分类相同的 开始进行推送   推送之前 先查询 是否记录表中存在数据
                WQbdMyPushRecord record = wQbdMyPushRecordService.findByUserIdandTzid(wQbdMyKnowledge.getUser_id(), wQbdQualityKnow.getId());
                if (BeanUtils.isNotEmpty(record)){
                    record.setPushStatus(HighlightConst.PUSH_YES);
                    wQbdMyPushRecordService.update(record);
                }else{
                    WQbdMyPushRecord record1 =new WQbdMyPushRecord();
                    record1.setId(UUIDUtils.getUUIDFor32());
                    record1.setPushStatus(HighlightConst.PUSH_YES);
                    record1.setKnowledgeId(wQbdQualityKnow.getId());
                    record1.setKnowledgeclassId(wQbdQualityKnow.getKnowledgeClassification());
                    record1.setCreateTime(new Date());
//                    record1.setCreateBy(userId);
                    record1.setUserid(wQbdMyKnowledge.getUser_id());
                    wQbdMyPushRecordService.add(record1);
                }
            });
        }
        if (result>0) {
            return success("成功");
        }else{
            return fail("失败");
        }
    }

}
